const app = getApp()
const util = require('../../utils/util.js');

Page({

  data: {
    loading: 1,
    navCurSign: 1,
    navCurLate: 2,
    currentTab: 0,
    status: 0 // 没有起止时间记录，1有开始时间没有结束时间，2都有，签到已截止继续签算迟到
  },

  onLoad: function(options) {

    let today = new Date()
    let month = today.getMonth() + 1 < 10 ? `0${today.getMonth() + 1}` : today.getMonth() + 1
    let date = today.getDate() < 10 ? `0${today.getDate()}` : today.getDate()
    today = `${today.getFullYear()}-${month}-${date}`
    if (options.type == 3 || options.type == 4) {
      this.setData({
        navCurSign: 0,
        navCurLate: 1
      })
    }

    this.setData({
      meetingId: options.meetingId,
      type: options.type,
      name: options.name,
      today: today,
      swiperHeight: app.globalData.windowHeight - 648
    })

    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getMeetingTime',
      method: 'POST',
      data: {
        meetingId: this.data.meetingId,
        meetingDate: this.data.today
      },
      dataType: 'json',
      success: res => {
        console.log(res)
        if (!res.data) {
          this.setData({
            status: 0
          })
        } else if (res.data.endTime) {
          this.setData({
            status: 2,
            startTime: res.data.startTime.split(" ")[1],
            endTime: res.data.endTime.split(" ")[1],
            swiperHeight: app.globalData.windowHeight - 660

          })
        } else {
          this.setData({
            status: 1,
            startTime: res.data.startTime.split(" ")[1],
            swiperHeight: app.globalData.windowHeight - 720
          })
        }
      },
      complete: () => {
        if(this.data.startTime){
          let search = setInterval(() => {
            this.getTodayList()
          }, 1000)
          this.setData({
            interval: search
          })
        }else{
          this.setData({
            loading: 0
          })
        }
   
   
      }
    })



  },

  onHide: function() {
    if (this.data.interval) {
      let search = this.data.interval
      clearInterval(search)
    }
  },

  onUnload: function() {
    if (this.data.interval) {
      let search = this.data.interval
      clearInterval(search)
    }
  },

  getTodayList() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getCheckConditionForWeChat',
      method: 'POST',
      data: {
        id: this.data.meetingId,
        createDate: this.data.today
      },
      dataType: 'json',
      success: res => {
        console.log(res)
        this.setData({
          todayList: res.data
        })
      },
      complete: () => {
        wx.hideLoading()
        this.setData({
          loading: 0
        })
      }
    })
  },

  startShowModal() {
    this.setData({
      modalName: 'DialogModal1'
    })
  },
  endShowModal() {
    this.setData({
      modalName: 'DialogModal2'
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  start() {
    this.hideModal()
    wx.showLoading({
      title: '正在开始',
      mask: true
    })
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/insertMeetingTime',
      method: 'POST',
      data: {
        meetingId: this.data.meetingId
      },
      dataType: 'json',
      success: res => {
        console.log(res)
        
        if (res.data.data == "fail") {
          wx.hideLoading()
          wx.showModal({
            content: '服务器异常，请稍后再进行签到时间配置',
            showCancel: false,
          })
        } else {
          this.setData({
            status: 1,
            startTime: res.data.data.split(" ")[1],
            swiperHeight: app.globalData.windowHeight - 720
          })

          let search = setInterval(() => {
            this.getTodayList()
          }, 1000)

          this.setData({
            interval: search
          })
        }

      },
      fail: () => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      }
    })
  },

  end() {
    this.hideModal()
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/addEndMeetingTime',
      method: 'POST',
      data: {
        meetingId: this.data.meetingId
      },
      dataType: 'json',
      success: res => {
        console.log(res)
        wx.hideLoading()
        if (res.data.data == "fail") {
          wx.showModal({
            content: '服务器异常,请稍后再试',
            showCancel: false,
          })
        } else {
          this.setData({
            status: 2,
            endTime: res.data.data.split(" ")[1],
            swiperHeight: app.globalData.windowHeight - 660
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      }
    })
  },
  bindChange: function(e) {
    console.log(e.detail.current)
    this.setData({
      currentTab: e.detail.current
    })
  },

  swichNav: function(e) {
    console.log(e.currentTarget.dataset.current)
    this.setData({
      currentTab: e.currentTarget.dataset.current
    })
  },
})